﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FTchina.Logic;
using FTchina.Library;

public partial class ui_Agency_2G : System.Web.UI.Page
{
    public string url = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        //url = "/ui/cquery/CQuery_showReport.aspx?id=36&ROBOTKEY=" + System.Configuration.ConfigurationManager.AppSettings["ROBOTKEY"].ToString();
        if (!IsPostBack)
        {
            BindGrid(false);
        }
    }

    /// <summary>
    /// 绑定表格
    /// </summary>
    private void BindGrid(bool IsToExcel)
    {



        string month = DateTime.Now.ToString("yyyyMM");
        string dpt = MasterHelper.EssAreaName;


        PWMIS.DataProvider.Data.AdoHelper ado = PWMIS.DataProvider.Adapter.MyDB.GetDBHelperByConnectionName("DYDB");
        //每页条数
        Grid1.PageSize = 20;


        try
        {
            //string testsql = "select count(*) from ww_ls_device";
            //ado.ExecuteScalar(testsql);




            string sql = @"select depart_id,代理商,渠道经理,
  count(case when to_char(a.develop_date,'yyyymm')='{0}' then a.user_id end) 当月提卡数量,
  count(case when to_char(a.develop_date,'yyyymm')='{0}' and flag='敞开打' then a.user_id end) 敞开打当月提卡数量,
  count(case when to_char(a.develop_date,'yyyymm')='{0}' and flag='融合' then a.user_id end) 融合当月提卡数量,
  count(case when acct_tag<>'0' then a.user_id end) 库存2G卡数量,
  count(case when acct_tag<>'0' and flag='敞开打' then a.user_id end) 敞开打库存数量,
  count(case when acct_tag<>'0' and flag='融合' then a.user_id end) 融合库存数量,
  count(case when acct_tag='0' and to_char(a.open_date,'yyyymm')='{0}' then a.user_id end) 当月2G激活数量,
  count(case when acct_tag='0' and to_char(a.open_date,'yyyymm')='{0}' and flag='敞开打' then a.user_id end) 敞开打当月激活数量,
  count(case when acct_tag='0' and to_char(a.open_date,'yyyymm')='{0}' and flag='融合' then a.user_id end) 融合当月激活数量
from ww_ls_device a
WHERE a.AREA_NAME='{1}'
group by depart_id,代理商,渠道经理";

            sql = string.Format(sql, month, dpt);
            // AlertLogic.ShowInfo(sql);
            if (IsToExcel)
            {//如果是导出Excel
                FTchina.OfficeHelper.ToExcel.DataSetToExcel(ado.ExecuteDataSet(sql));
                return;
            }
            int count = ado.ExecuteDataSet(sql).Tables[0].Rows.Count;
            string single = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.Oracle, sql, "1=1", Grid1.PageSize, Grid1.PageIndex + 1, count);


            Grid1.RecordCount = count;
            Grid1.DataSource = ado.ExecuteDataSet(single);
            Grid1.DataBind();

        }
        catch (Exception e)
        {
            AlertLogic.ShowError("正在更新数据，请稍候……");
            return;
        }

    }
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Grid1_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {

        Grid1.PageIndex = e.NewPageIndex;
        BindGrid(false);
    }
    protected void btnExcel_click(object sender, EventArgs e)
    {
        BindGrid(true);
    }
}